﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RestNews.Data.Models;

namespace RestNews.Models
{
    /// <summary>
    /// User class
    /// </summary>
    public class NewsUser
    {
        public long Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public IList<string> Roles { get; set; }
        public string Address { get; set; }
        public string Username { get; set; }

        /// <summary>
        /// Gets the user.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static NewsUser GetUser(RestNewsContext db, long id)
        {
            Person person = db.People.FirstOrDefault(p => p.Id == id);

            //Return null of there is no user
            if (person == null)
            {
                return null;
            }

            NewsUser user = new NewsUser
            {
                Id = person.Id,
                FirstName = person.FirstName,
                LastName = person.LastName,
                Address = person.Address,
                Roles = person.User.Roles.Select(ur => ur.Name).ToList(),
                Username = person.User.Username
            };
            User x = new User();
            
            return user;
        }

        
    }
}